package com.didispace.chapter310.service;

import com.didispace.chapter310.User;
import com.didispace.chapter310.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TestImpl implements Test {
    @Autowired
    private UserRepository userRepository;
    @Override
    @Transactional
    public void test1() {
       this.test123();
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class,readOnly = false,value="transactionManager",propagation= Propagation.REQUIRES_NEW)
    public void test2() {
        User a = new User("A", 10);
        System.out.println(a.getName());
        System.out.println(a.getName().length());
        userRepository.save(a);
        throw new RuntimeException("test");
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class,readOnly = false,value="transactionManager",propagation= Propagation.REQUIRED)
    public void test3() {
        User a = new User("A", 10);
        System.out.println(a.getName());
        System.out.println(a.getName().length());
        userRepository.save(a);
        throw new RuntimeException("test");
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class,readOnly = false,value="transactionManager",propagation= Propagation.NEVER)
    public void test4() {
        User a = new User("A", 10);
        System.out.println(a.getName());
        System.out.println(a.getName().length());
        userRepository.save(a);
       /// throw new RuntimeException("test");
    }

    public void test123(){
        User a = new User("A", 10);
        System.out.println(a.getName());
        System.out.println(a.getName().length());
        userRepository.save(a);
        throw new RuntimeException("test");
    }

}
